package com.pl.rabarbar.threads;

import javax.servlet.ServletContextEvent;

import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextListener;
import javax.ws.rs.core.MediaType;

import com.pl.rabarbar.convert.ConvertJson;
import com.pl.rabarbar.model.Book;
import com.pl.rabarbar.services.GetFromServer;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

public class Notificator extends TimerTask implements ServletContextListener {
	private Thread thread;
	private List idList;
	String responseFromServer;

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		this.thread.interrupt();

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		thread = new Thread(this);
		thread.start();

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub

		while (true) {

			try {
				System.out.println("RUN RUN RUN \n");
//Book book = new Book(4,"aa","aaaa", 10);
String responseFromServer = GetFromServer.getFromServer( "http://localhost:8080/BookstoreTest/bookstore/getAllId");

this.idList = ConvertJson.convertFromJsonToList(responseFromServer);	
ClientConfig clientConfig = new DefaultClientConfig();

				clientConfig.getFeatures().put(
						JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

				Client client = Client.create(clientConfig);

				WebResource webResource = client
						.resource("http://localhost:9090/BookstoreClient/get/send");

				 ClientResponse response =
				 webResource.type("application/json").post(ClientResponse.class, this.idList);

				//ClientResponse response = webResource.accept(
			//			MediaType.TEXT_PLAIN).post(ClientResponse.class, st);
					
				if (response.getStatus() != 200) {
					throw new RuntimeException("Failed : HTTP error code : "
							+ response.getStatus());
				}
				Thread.sleep(600000);
				String output = response.getEntity(String.class);

				System.out.println("Server response to us! \n");
				System.out.println(output);
			

			} catch (Exception e) {

				e.printStackTrace();

			}

			/*
			 * try { String url =
			 * "http://localhost:9090/BookstoreClient/getyourbook/updatelist";
			 * 
			 * URL obj = new URL(url); HttpURLConnection con =
			 * (HttpURLConnection) obj.openConnection(); String
			 * responseFromServer = GetFromServer.getFromServer(
			 * "http://localhost:8080/BookstoreTest/bookstore/getAllId"); //
			 * optional default is GET con.setRequestMethod("POST");
			 * con.setDoInput(true); con.setDoOutput(true); DataOutputStream dos
			 * = new DataOutputStream(con.getOutputStream());
			 * dos.writeBytes(responseFromServer);
			 * 
			 * TimeUnit.MINUTES.sleep(10); } catch (InterruptedException |
			 * IOException e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); } /* pobieranie danych co 10 minut
			 */
		}

	}

}
